<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>生命周期</title>
    <script src="../commonjs/vue.js"></script>
</head>
<body>
    <div id="root">
        <h2 :style="{opacity}">Welcome to study Vue2!</h2>
        <button @click="opacity = 1">透明度设置为1</button>
        <button @click="stop">点我停止变换</button>
    </div>
    <script>
        Vue.config.productionTip = false;//关闭开发提示
        const vm = new Vue({
            el:'#root',
            data:{
                opacity:1
            },
            methods:{
                //内部函数实现(推荐)
                change(){
                    console.log("change()")
                    this.timer = setInterval(()=>{
                        this.opacity -= 0.01
                        if (this.opacity <= 0) this.opacity = 1
                    },16)
                },
                stop(){
                    // clearInterval(this.timer)  //不推荐
                    this.$destroy()
                }
            },
            //vue完成模板解析并把所有真实DOM载入页面后调用mounted(挂载)
            mounted(){
                this.change()
            },
            beforeDestroy() {
                console.log("vm即将销毁...")
                clearInterval(this.timer)
            }
        })
        //外部函数实现(不推荐)
/*        setInterval(()=>{
            vm.opacity -= 0.01
            if (vm.opacity <= 0) vm.opacity = 1
        },16)*/
    </script>
</body>
</html>